package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.constant.UserConstant;
import net.wanho.dto.AddAuthDTO;
import net.wanho.dto.AuthInfoDTO;
import net.wanho.po.AuthInfo;
import net.wanho.service.AuthInfoService;
import net.wanho.vo.AjaxResult;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/auth")
public class AuthInfoController {

    private final AuthInfoService authInfoService;

    private final RedisTemplate<String, Object> redisTemplate;
    @GetMapping("/auth-tree")
    public AjaxResult authTree() {
        List<AuthInfo> authInfoList = authInfoService.findAll();
        return AjaxResult.success("success", authInfoList);
    }

    @GetMapping("/name-check")
    public AjaxResult nameCheck(@RequestParam String authName) {
        Boolean nameCheck = authInfoService.nameCheck(authName);
        return AjaxResult.success("success", nameCheck);
    }

    @GetMapping("/url-check")
    public AjaxResult urlCheck(@RequestParam String authUrl) {
        Boolean urlCheck = authInfoService.urlCheck(authUrl);
        return AjaxResult.success("success", urlCheck);
    }

    @GetMapping("/code-check")
    public AjaxResult codeCheck(@RequestParam String authCode) {
        Boolean codeCheck = authInfoService.selectByCode(authCode);
        return AjaxResult.success("success", codeCheck);
    }

    @PostMapping("/auth-add")
    public AjaxResult authAdd(@RequestBody AddAuthDTO addAuthDTO) {
        authInfoService.addAuth(addAuthDTO);
        return AjaxResult.success("权限添加成功");
    }

    @PutMapping("/auth-enable/{id}")
    public AjaxResult authEnable(@PathVariable Integer id) {
        authInfoService.enableAuth(id);
        return AjaxResult.success("权限启用成功");
    }

    @PutMapping("/auth-disable/{id}")
    public AjaxResult authDisable(@PathVariable Integer id) {
        authInfoService.disableAuth(id);
        return AjaxResult.success("权限禁用成功");
    }

    @PutMapping("/auth-update")
    public AjaxResult authUpdate(@RequestBody AuthInfoDTO authInfoDTO) {
        authInfoService.updateAuth(authInfoDTO);
        return AjaxResult.success("权限更新成功");
    }

    @DeleteMapping("/auth-delete/{id}")
    public AjaxResult authDelete(@PathVariable Integer id) {
        authInfoService.removeById(id);
        // 删除权限后，需要删除Redis中缓存的所有菜单数据！！
        redisTemplate.delete(UserConstant.MENU_PREFIX);
        return AjaxResult.success("权限删除成功");
    }
}
